<template>
  <view class="login">
    <view class="box">
      <el-input placeholder="请输入账号..." v-model="account" clearable></el-input>
      <el-input placeholder="请输入密码..." v-model="password" show-password></el-input>
      <el-button @click="login()">登录</el-button>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        account: '666',
        password: '123456'
      }
    },
    methods: {
      login() {
        uni.request({
          url: '/api/consumers/login',
          method: 'POST',
          data: {
            account: this.account,
            password: this.password
          },
          success: (res) => {
            // console.log(res.data)
            if (res.data.code == 1) { // 1 登录成功 0 失败
              let consumer = res.data.data
              uni.setStorageSync('id', consumer.id)
              uni.setStorageSync('account', consumer.tel)
              uni.setStorageSync('name', consumer.name) // 昵称
              uni.setStorageSync('profile', consumer.profile)
              this.$message.success('登录成功')
              uni.switchTab({
                url: '/pages/index/index'
              })
            } else {
              this.$message.error('账号或用户名错误')
            }
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .login {
    // background: #f6f6f6;
    width: 100%;
  }

  .box {
    width: 85%;
    margin: 0 auto;
    padding: 50rpx 0;

    .el-input {
      margin: 20rpx auto;
    }

    .el-button {
      display: block;
      width: 200rpx;
      margin: 20rpx auto;
    }
  }
</style>